﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Data;
using DevExpress.Web.ASPxGridView;

public partial class Store_Manager_SM_EditPO : System.Web.UI.Page
{
    string order;
    string proname;
    decimal qty;
    
    string Stockcode;
    protected void Page_Load(object sender, EventArgs e)
    {

        ASPxGridView2.TotalSummary.Clear();
        
        ASPxLabel17.Visible = false;
        ASPxLabel7.Visible = false;
        ASPxLabel8.Visible = false;
        ASPxLabel6.Visible = false;
        ASPxLabel9.Visible = false;
        ASPxLabel10.Visible = false;
        ASPxLabel11.Visible = false;
        ASPxLabel12.Visible = false;
        ASPxLabel3.Visible = false;
        ASPxLabel4.Visible = false;
        ASPxLabel15.Visible = false;
        ASPxLabel16.Visible = false;
        string b=Request.QueryString.ToString();
        
        if (b != "")
        {
            ASPxLabel5.Visible = true;
            ASPxLabel4.Visible = false;
            ASPxLabel5.Text = "Order Approved Successfully";
        }
        else
        {
            if (!IsPostBack)
            {
                ASPxGridView2.DataBind();
                Session["update"] = null;
                ASPxSummaryItem totalSummary = new ASPxSummaryItem();
                totalSummary.FieldName = "Total";
                totalSummary.ShowInColumn = "Total";
                totalSummary.SummaryType = SummaryItemType.Sum;
                ASPxGridView2.TotalSummary.Add(totalSummary);

                decimal grnlast = Bestlife.ERP.BusinessRules.B_GRN.lastapproveid();
                DropDownList1.SelectedIndex = -1;
                DropDownList1.Text = grnlast.ToString();
                supplier_details(grnlast.ToString());
                ASPxLabel7.Visible = true;
                ASPxLabel8.Visible = true;
                ASPxLabel6.Visible = true;
                ASPxLabel9.Visible = true;
                ASPxLabel10.Visible = true;
                ASPxLabel11.Visible = true;
                ASPxLabel12.Visible = true;
                ASPxLabel3.Visible = true;
                ASPxLabel4.Visible = true;
                ASPxLabel15.Visible = true;
                ASPxLabel16.Visible = true;
                ASPxLabel17.Visible = false;
                ASPxLabel5.Visible = false;
                ASPxLabel4.Visible = false;

                
                
                //ASPxTextBox1.Text = Bestlife.ERP.BusinessRules.B_GRN.grandtotal(DropDownList1.Text).ToString();
            }
        }
       
        Session["current"] = "GRN to Approve";//current page
        if (Session["user"] != null)//check user
        {
            string user = Session["user"].ToString();
            if (user == "viewer")
            {
                Response.Redirect("index.aspx");
            }

        }
        else
        {
            Response.Redirect("index.aspx");
        }
    }
    protected void ASPxGridView2_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        //Bestlife.ERP.Entities.E_PurchaseOrder order = new Bestlife.ERP.Entities.E_PurchaseOrder();
        //order.Approval = Convert.ToString(e.NewValues[""]);
        //order.Contrat = Convert.ToString(e.NewValues[""]);
        //order.Description = Convert.ToString(e.NewValues[""]);
        //order.Due = Convert.ToDateTime(e.NewValues[""]);
        //order.Issu = Convert.ToDateTime(e.NewValues[""]);
        //order.Or_ID1 = Convert.ToString(e.NewValues[""]);
        //order.Proname = Convert.ToString(e.NewValues[""]);
        //order.Qty = Convert.ToDecimal(e.NewValues[""]);
        //order.Supplier = Convert.ToString(e.NewValues[""]);
        //order.Total = Convert.ToDecimal(e.NewValues[""]);
        //order.Unitvalue = Convert.ToDecimal(e.NewValues[""]);

        //Bestlife.ERP.BusinessRules.

    }
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {

        List<Bestlife.ERP.Entities.E_GRN> listgrn = new List<Bestlife.ERP.Entities.E_GRN>();
        
        int i = ASPxGridView2.VisibleRowCount;
         Boolean result=false;
        List<Object> itemid1 = ASPxGridView2.GetSelectedFieldValues("Pro_ID");
        List<Object> itemid = ASPxGridView2.GetSelectedFieldValues("Or_ID");
        List<Object> itemid2 = ASPxGridView2.GetSelectedFieldValues("Qty");
       
        List<Object> itemid4 = ASPxGridView2.GetSelectedFieldValues("Sto_ID");
        int j = itemid.Count;
        string user = Session["username"].ToString();
        if (DropDownList2.SelectedValue != "")
        {
            if (DropDownList2.SelectedValue == "YES")
            {
                string approval = DropDownList2.SelectedValue;
                if (itemid.Count == 0 || itemid1.Count == 0 || itemid2.Count == 0 ||itemid4.Count == 0)
                {
                    ASPxLabel4.Visible = true;
                    ASPxLabel4.Text = "Please select order items ";
                }
                else
                {
                    foreach (object selectitemid in itemid)
                    {
                        order = selectitemid.ToString();

                    }
                        foreach (object selectitemid1 in itemid1)
                        {
                            proname = selectitemid1.ToString();

                        }
                            foreach (object selectitemid2 in itemid2)
                            {
                                qty = decimal.Parse(selectitemid2.ToString());
                              
                            }
                            
                            foreach (object selectitemid4 in itemid4)
                            {
                                Stockcode = selectitemid4.ToString();

                            }
                            for (int k = 0; k < itemid.Count; k++)
                            {
                                Bestlife.ERP.Entities.E_GRN grn = new Bestlife.ERP.Entities.E_GRN();
                                grn.Stockid = itemid4[k].ToString();
                               
                                grn.Order = itemid[k].ToString();
                                grn.Proname = itemid1[k].ToString();
                                grn.Qty =decimal.Parse(itemid2[k].ToString());
                                listgrn.Add(grn);
                                
                            }
                            if (Session["update"] != null)
                            {
                                string user1 = Session["username"].ToString();
                                ASPxSummaryItem totalSummary = new ASPxSummaryItem();
                                totalSummary.FieldName = "Total";
                                totalSummary.SummaryType = SummaryItemType.Sum;
                                ASPxGridView2.TotalSummary.Add(totalSummary);
                                string totvalue = ASPxGridView2.GetTotalSummaryValue(totalSummary).ToString();
                                Bestlife.ERP.Entities.E_PurchaseOrder grn = new Bestlife.ERP.Entities.E_PurchaseOrder();
                                grn.Total = Convert.ToDecimal(totvalue);
                                grn.Or_ID1 = DropDownList1.Text;
                                Bestlife.ERP.BusinessRules.B_GRN.upd_GRNbytot(grn, user1);
                            }
                        }
                string approve = "YES";
                if (i == j)
                {


                    result = Bestlife.ERP.BusinessRules.B_PurchaseOrder.allapprove(listgrn, user, approve, itemid.Count);
                    if (result == false)
                    {
                        ASPxLabel4.Visible = true;
                        ASPxLabel4.Text = "Error in the Process";
                    }

                }
                else
                {
                    string approve1;
                    int countrow = ASPxGridView2.VisibleRowCount;
                    int listcount = listgrn.Count;
                    if (countrow == listcount)
                    {
                        approve1 = "YES";
                    }
                    else
                    {
                        approve1 = "HALF";
                    }

                    foreach (Bestlife.ERP.Entities.E_GRN listgn in listgrn)
                    {

                        result = Bestlife.ERP.BusinessRules.B_PurchaseOrder.approval(listgn, user, approve1, itemid.Count);
                    }

                }
                if (result)
                {

                    Response.Redirect("SM_ApprovGRN.aspx?a=1");
                }
                    }

           
            }
            else
            {
                ASPxLabel4.Visible = true;
                ASPxLabel4.Text = "Please select one option for approve ";
            }

           
                   

                }
             
    public void supplier_details(string grnno)
    {
         Bestlife.ERP.Entities.E_GRN grn = new Bestlife.ERP.Entities.E_GRN();
         grn = Bestlife.ERP.BusinessRules.B_GRN.getGRNdetails(grnno);
      
        //DropDownList3.Text = grn.Supplier;
        ASPxLabel17.Text = grn.Billno;
        ASPxLabel16.Text = grn.Supplier;
        ASPxLabel14.Text = grn.Recevieddate;
        ASPxLabel15.Text = grn.Duedate;
        ASPxLabel11.Text = grn.User;
        ASPxLabel13.Text = grn.Datecreate;
    }
    protected void ASPxGridView2_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
    {
        if (e.Column.FieldName == "Total")
        {
            decimal unitprice = Convert.ToDecimal(e.GetListSourceFieldValue("Pur_Price"));
            decimal Quntity = Convert.ToDecimal(e.GetListSourceFieldValue("Qty"));
            decimal tot = unitprice * Quntity;
            e.Value = tot;

        }
       
    }
    protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {

       // ASPxTextBox1.Text = Bestlife.ERP.BusinessRules.B_GRN.grandtotal(DropDownList1.SelectedValue).ToString();
        supplier_details(DropDownList1.Text);
        ASPxLabel7.Visible = true;
        ASPxLabel8.Visible = true;
        ASPxLabel6.Visible = true;
        ASPxLabel9.Visible = true;
        ASPxLabel10.Visible = true;
        ASPxLabel11.Visible = true;
        ASPxLabel12.Visible = true;
        ASPxLabel3.Visible = true;
        ASPxLabel4.Visible = true;
        ASPxLabel15.Visible = true;
        ASPxLabel16.Visible = true;
        ASPxLabel17.Visible = false;
        ASPxLabel5.Visible = false;
        ASPxLabel4.Visible = false;
        ASPxSummaryItem totalSummary = new ASPxSummaryItem();
        totalSummary.FieldName = "Total";
        totalSummary.ShowInColumn = "Total";
        totalSummary.SummaryType = SummaryItemType.Sum;
        totalSummary.DisplayFormat = "Total Rs.{0}";
        ASPxGridView2.TotalSummary.Add(totalSummary);

    }
    protected void ASPxGridView2_RowUpdating1(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {

        Session["update"] = "1";
        ASPxSummaryItem totalSummary = new ASPxSummaryItem();
        totalSummary.FieldName = "Total";
        totalSummary.ShowInColumn = "Total";
        totalSummary.SummaryType = SummaryItemType.Sum;
        totalSummary.DisplayFormat = "Total Rs.{0}";
        ASPxGridView2.TotalSummary.Add(totalSummary);
    }
    protected void ASPxGridView2_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        Session["update"] = "1";
        ASPxSummaryItem totalSummary = new ASPxSummaryItem();
        totalSummary.FieldName = "Total";
        totalSummary.ShowInColumn = "Total";
        totalSummary.SummaryType = SummaryItemType.Sum;
        totalSummary.DisplayFormat = "Total Rs.{0}";
        ASPxGridView2.TotalSummary.Add(totalSummary);
    }
}
       
